#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>

void* mythread(void* arg) {
    printf("子线程, 其所在进程: pid = [%d], 线程id = [%ld]\n", getpid(), pthread_self());
    sleep(10);
}

int main()
{
    // int pthread_create(pthread_t *thread,  const pthread_attr_t *attr,   void *(*start_routine) (void *),  void *arg);
    pthread_t thr;
    int ret = pthread_create(&thr, NULL, mythread, NULL);
    if(ret != 0) {
        printf("pthread_create err: [%s]\n", strerror(ret));
        return -1;
    }
    
    pthread_detach(thr);
    
    // pthread_join() 不再阻塞，立马返回
    ret = pthread_join(thr, NULL);
    if(ret != 0) {
        printf("pthread_join error: [%s]\n", strerror(ret));
    }

    printf("主线程, 其所在进程: pid = [%d], 主线程 id = [%ld]\n", getpid(), pthread_self());

    sleep(1);   // 确保子线程能执行
    return 0;
}

